#include "curl.h"
#include <stdio.h>
#include "cJSON.h"

size_t writeCallback(void *contents, size_t size, size_t nmemb, void *userp) {
    size_t realsize = size * nmemb;
    // 这里可以处理获取到的数据，比如存储到文件或者其他操作
    char * result = (char *)contents;
    printf("%s\n",result);

    cJSON *jsonstr= cJSON_Parse(result);
    if(jsonstr!=NULL){
        cJSON *successRoot = cJSON_GetObjectItemCaseSensitive(jsonstr,"success");
        cJSON *codeRoot = cJSON_GetObjectItemCaseSensitive(jsonstr,"code");
        cJSON *messageRoot = cJSON_GetObjectItemCaseSensitive(jsonstr,"message");
        
        if(cJSON_IsTrue(successRoot)){
            printf("%s\n",messageRoot->valuestring) ;    
        }else{
            perror("网络异常\n");
        }



    }





    return realsize;
}


int main(int argc, char const *argv[])
{
    //1.初始化一个curl
    CURL *curl= curl_easy_init();
    
    //2.给curl设置一些对应的选项（目标地址，回调函数，请求参数，发送文件）
    curl_easy_setopt(curl,CURLOPT_URL,"http://47.109.65.208/config.json");

    //3.发送请求参数
    //curl_easy_setopt(curl,CURLOPT_POSTFIELDS,"xx=ssas&sadas=sds");
    //4.设置回调函数
    curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,writeCallback);

    //5.发送请求
    CURLcode code = curl_easy_perform(curl);

    if (code!=CURLE_OK )
    {
         fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(code));
    }
    
    //6.清理
    curl_easy_cleanup(curl);

    return 0;
}
